package com.np.controller;

import com.google.gson.Gson;
import com.np.WebSettings;
import com.np.model.OrderSheet;
import com.np.model.Product;
import com.np.model.User;
import com.np.util.HibernateUtil;
import com.sun.deploy.net.HttpResponse;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.List;

/**
 * 接口
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    @RequestMapping("/test")
    public String test(){
        User u = new User();
        u.setId(100);
        u.setLevel(2);
        u.setName("wyf");
        u.setAddress("123456");
        return "11111";
    }

    @RequestMapping(value = "/productlist",produces="text/html;charset=UTF-8")
    public String productList(){

        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Product ";
        Query query = session.createQuery(hql);
        List<Product> list = query.list();
        Gson gson = new Gson();
        System.out.print(gson.toJson(list));
        return gson.toJson(list);
    }


    @RequestMapping(value = "/user",produces="text/html;charset=UTF-8")
    public String user(){
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from User ";
        Query query = session.createQuery(hql);
        List<User> list = query.list();
        Gson gson = new Gson();
        System.out.print(gson.toJson(list));
        return gson.toJson(list);
    }

   // @ResponseBody
    @RequestMapping(value = "/addorder",produces="text/html;charset=UTF-8")
    public String addOrder(OrderSheet orderSheet){
        if(orderSheet==null){
          return  "1";
        }

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(orderSheet);
        session.getTransaction().commit();

        return "0";

    }


    /**
     * 文件下载
     * @Description:
     * @param fileName
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/download")
    public String downloadFile(@RequestParam("fileName") String fileName,
                               HttpServletRequest request, HttpServletResponse response) {
        if (fileName != null) {
            String realPath = request.getServletContext().getRealPath(
                    "WEB-INF/File/");
            fileName.replace("../img/","");
            System.out.println("*****************************************************"+fileName);
            File file = new File(WebSettings.IMAGE_URL, fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition",
                        "attachment;fileName=" + fileName);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }
}
